  <!--
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2020-08-31 09:02:17
 * @LastEditors: Sam
 * @LastEditTime: 2020-12-11 19:56:34
-->

<script>
    /**
     * @name: 单图上传
     * @Author: Sam
     * @Date: 2020-08-31 15:04:35
     * @msg: 
     * @param {type} 
     * @return {type} 
     */ 
    function upload_img(inputname = 'thumb',opt){
         opt === undefined && (opt = {});
         var area=[(opt.w || 1200)+'px', (opt.h || 600)+'px'];
     return layer.open({
             type: 2,
             title:'图片选择',
             area: area,
             fixed: false, //不固定
             maxmin: true,
             moveOut:false,//true  可以拖出窗外  false 只能在窗内拖
             anim:5,//出场动画 isOutAnim bool 关闭动画
             offset:'auto',//['100px','100px'],//'auto',//初始位置  ['100px','100px'] t[ 上 左]
             shade:0,//遮罩
             resize:true,//是否允许拉伸
             // content: "{:url('admin/widget.images/index',['fodder'=>"+inputname+",'nums_type'=>1])}",//内容
             content: "{:url('admin/widget.images/index')}"+'?fodder='+inputname+'&nums_type=1',//内容
             move:'.layui-layer-title'// 默认".layui-layer-title",// 触发拖动的元素
         });
    }
    /**
     * @name: 多图上传
     * @Author: Sam
     * @Date: 2020-08-31 15:05:32
     * @msg: 
     * @param {type} 
     * @return {type} 
     */
    function upload_imgs(inputname = 'thumb',num =6,opt){
         opt === undefined && (opt = {});
         var area=[(opt.w || 1200)+'px', (opt.h || 600)+'px'];
     return layer.open({
             type: 2,
             title:'图片选择',
             area: area,
             fixed: false, //不固定
             maxmin: true,
             moveOut:false,//true  可以拖出窗外  false 只能在窗内拖
             anim:5,//出场动画 isOutAnim bool 关闭动画
             offset:'auto',//['100px','100px'],//'auto',//初始位置  ['100px','100px'] t[ 上 左]
             shade:0,//遮罩
             resize:true,//是否允许拉伸
             content: "{:url('admin/widget.images/index')}"+'?fodder='+inputname+'&nums_type=2'+'&nums='+num,//内容
             move:'.layui-layer-title'// 默认".layui-layer-title",// 触发拖动的元素
         });
    }
 
    /**
     * @name: 单图回调
     * @Author: Sam
     * @Date: 2020-08-31 15:12:37
     * @msg: 
     * @param {type} 
     * @return {type} 
     */
    function changeIMG(inputname,src){
        
        $('#albumList_'+inputname).empty();
        $('#albumList_'+inputname).append('<div class="album">' +
                         '    <img layer-src ="' + src + '" src="' + src + '" alt=""   data-tips-image>' +
                         '    <input type="hidden" name="'+inputname+'" value="' + src + '">' +
                         '    <p class="layui-btn layui-btn-normal" style="display: block;" onclick="del_img(this)">删除</p>' +
                         '</div>');
            }
     
    
    /**
     * @name: 多图回调
     * @Author: Sam
     * @Date: 2020-08-31 15:12:44
     * @msg: 
     * @param {type} 
     * @return {type} 
     */
    function changeIMGS(inputname,src){
     for (let index = 0; index < src.length; index++) {
             $('#albumLists_'+inputname).append('<div class="album">' +
                         '    <img layer-src ="' + src[index] + '" src="' + src[index] + '" alt="" data-tips-image>' +
                         '    <input type="hidden" name="'+inputname+'[]" value="' + src[index] + '">' +
                         '    <p class="layui-btn layui-btn-normal" style="display: block;" onclick="del_img(this)">删除</p>' +
                         '</div>');
            }
    }
        //删除图片
     function del_img(obj) {
         $(obj).parent().remove();
     }
 
 $(function(){
     var form = layui.form;
     form.render();
       //监听提交
     form.on('submit(ajax_form_submit)', function(data){
         var url    = data.form.action;
         var method = data.form.method;
         var field  = data.field;
         $.ajax({
             url  : url,
             type : method,
             data : field,
             dataType:'JSON',
             beforeSend:function(){
                 load = layer.load(1); //0代表加载的风格，支持0-2
                 $('.ajax_form_submit').hide();
             },
             success:function(res){
                
                 if(res.code == 200 || res.code ==1){
                     layer.msg(res.msg,{icon: 1,time: 1500},function () {
                             window.parent.layer.closeAll();
                             window.parent.layList.reload();
                         })
                 }else{
                     layer.msg(res.msg,{icon: 2,time: 1000});
                 }
             },
             complete:function(){
                 layer.close(load);
                 $('.ajax_form_submit').show();
             },
             error:function(){
                 layer.alert('服务器繁忙，请稍候');
             },
             
         });
         return false;
       });
     })
     $('body').on('click', '[data-tips-image]', function () {
             let content = $(this).attr('src');
             layer.open({
                 type:1,
                 title:false,
                 closeBtn:1,
                 skin:'layui-layer-nobg',
                 shadeClose:true,
                 content:'<img src='+content+'>',
                 
             })
         });
     $('body').on('click', '[data-confirm]', function () {
         var content = $(this).data('url');
         var id      = $(this).data('id') ?? '';
         layer.confirm('确认操作么?',{icon:3,title:false},function(){
             $.ajax({
             url  : content,
             type : 'POST',
             dataType:'JSON',
             data :{id:id},
             beforeSend:function(){
                 load = layer.load(1); //0代表加载的风格，支持0-2
             },
             success:function(res){
               
                 if(res.code == 200 || res.code ==1){
                     layer.msg(res.msg,{icon: 1,time: 1500},function () {
                             window.parent.layer.closeAll();
                             window.parent.layList.reload();
                         })
                 }else{
                     layer.alert(res.msg,{icon: 2,time: 1000,title:'警告',time:0});
                 }
             },
             complete:function(){
                 layer.close(load);
             },
             error:function(){
                 layer.alert('服务器繁忙，请稍候');
             },
             
         });
         });
     });
     /**
      * @name: 打开新窗口
      * @Author: Sam
      * @Date: 2020-09-01 10:31:36
      * @msg: 
      * @param {type} 
      * @return {type} 
      */    
     function op_iframe(title,src,opt){
        
         opt === undefined && (opt = {});
         // var area=[(opt.w || 950)+'px', (opt.h || 560)+'px'];
         var area=[(opt.w || 80)+'%', (opt.h || 80)+'%'];
  
         return this.layer.open({
             type: 2,
             title:title,
             area: area,
             fixed: false, //不固定
             maxmin: true,
             moveOut:false,//true  可以拖出窗外  false 只能在窗内拖
             anim:5,//出场动画 isOutAnim bool 关闭动画
             offset:'auto',//['100px','100px'],//'auto',//初始位置  ['100px','100px'] t[ 上 左]
             shade:0,//遮罩
             resize:true,//是否允许拉伸
             content: src,//内容
             move:'.layui-layer-title'// 默认".layui-layer-title",// 触发拖动的元素
         });
     };
 
     /**
      * @name: 下拉选择
      * @Author: Sam
      * @Date: 2020-09-03 10:41:50
      * @msg: 
      * @param {type} 
      * @return {type} 
      */
     function dropdown(that) {
         var oEvent = arguments.callee.caller.arguments[0] || event;
         oEvent.stopPropagation();
         var offset = $(that).offset();
         var top = offset.top - $(window).scrollTop();
         var index = $(that).parents('tr').data('index');
         $('.layui-nav-child').each(function (key) {
             if (key != index) {
                 $(this).hide();
             }
         })
         if ($(document).height() < top + $(that).next('ul').height()) {
             $(that).next('ul').css({
                 'padding': 10,
                 'top': - ($(that).parent('td').height() / 2 + $(that).height() + $(that).next('ul').height() / 2),
                 'min-width': 'inherit',
                 'position': 'absolute'
             }).toggle();
         } else {
             $(that).next('ul').css({
                 'padding': 10,
                 'top': $(that).parent('td').height() / 2 + $(that).height(),
                 'min-width': 'inherit',
                 'position': 'absolute'
             }).toggle();
         }
     }
 </script>
 